package com.yijiaw.service;

import com.yijiaw.dao.UsDAO;

import com.yijiaw.domain.Us;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for Us entities
 * 
 */

@Service("UsService")
@Transactional
public class UsServiceImpl implements UsService {

	/**
	 * DAO injected by Spring that manages Us entities
	 * 
	 */
	@Autowired
	private UsDAO usDAO;

	/**
	 * Instantiates a new UsServiceImpl.
	 *
	 */
	public UsServiceImpl() {
	}

	/**
	 * Load an existing Us entity
	 * 
	 */
	@Transactional
	public Set<Us> loadUss() {
		return usDAO.findAllUss();
	}

	/**
	 * Return all Us entity
	 * 
	 */
	@Transactional
	public List<Us> findAllUss(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<Us>(usDAO.findAllUss(startResult, maxRows));
	}

	/**
	 * Delete an existing Us entity
	 * 
	 */
	@Transactional
	public void deleteUs(Us us) {
		usDAO.remove(us);
		usDAO.flush();
	}

	/**
	 * Save an existing Us entity
	 * 
	 */
	@Transactional
	public void saveUs(Us us) {
		Us existingUs = usDAO.findUsByPrimaryKey(us.getId());

		if (existingUs != null) {
			if (existingUs != us) {
				existingUs.setId(us.getId());
				existingUs.setName(us.getName());
				existingUs.setPhone(us.getPhone());
				existingUs.setAddress(us.getAddress());
				existingUs.setFax(us.getFax());
				existingUs.setContact(us.getContact());
				existingUs.setEmail(us.getEmail());
			}
			us = usDAO.store(existingUs);
		} else {
			us = usDAO.store(us);
		}
		usDAO.flush();
	}

	/**
	 */
	@Transactional
	public Us findUsByPrimaryKey(Integer id) {
		return usDAO.findUsByPrimaryKey(id);
	}

	/**
	 * Return a count of all Us entity
	 * 
	 */
	@Transactional
	public Integer countUss() {
		return ((Long) usDAO.createQuerySingleResult("select count(o) from Us o").getSingleResult()).intValue();
	}
}
